import pygame, math, sys, random

class Player(pygame.sprite.Sprite):
    def __init__(self, pos, screenSize):
        pygame.sprite.Sprite.__init__(self, self.containers)
        self.images = []
        imgs = ["rsc/imgs/player/player_right.png", "rsc/imgs/player/player_right_walk1.png", "rsc/imgs/player/player_right_walk2.png", 
        "rsc/imgs/player/player_left.png", "rsc/imgs/player/player_left_walk1.png", "rsc/imgs/player/player_left_walk2.png",
        "rsc/imgs/player/player_left_shoot1.png", "rsc/imgs/player/player_left_shoot2.png",
        "rsc/imgs/player/player_right_shoot1.png", "rsc/imgs/player/player_right_shoot2.png"]
        
        for img in imgs:
            surf = pygame.image.load(img)
            x = surf.get_width()
            y = surf.get_height()
            surf = pygame.transform.scale(surf, (int(x*2.5),int(y*2.5)))
            self.images += [surf]
        self.rightstill = self.images[0:1]
        self.rightimgs = self.images[1:3]
        self.leftstill = self.images[3:4]
        self.leftimgs = self.images[4:6]
        self.shootleft = self.images[6:8]
        self.shootright = self.images[8:10]
        self.images = self.rightstill
        self.frame = 0
        self.maxFrame = len(self.images)-1
        self.image = self.images[self.frame]
        self.rect = self.image.get_rect()
        self.lastRect = pygame.Rect(self.rect)
        self.maxSpeed = 7
        self.speed = [0,0]
        self.waitCount = 0
        self.waitMax = 4
        self.place(pos)
        self.placed = False
        self.screenWidth = screenSize[0]
        self.screenHeight = screenSize[1]
        self.living = True
        self.dir = "stop right"
        self.moving = False
        self.living = True
        self.onGround = False
        self.canJump = 0
        self.justJump = True
        self.floor = 0
        self.health = 10
        self.x = pos[0]
        self.y = pos[1]
        self.realx = pos[0]
        self.realy = pos[1]
        self.lastx = self.realx
        self.lasty = self.realy
        self.yScrollThreshold = 0
        self.xScrollThreshold = 150
        self.xScrolling = False
        self.yScrolling = False
        self.count = 0
        self.canShoot = False
        self.pos = [0,0]
        self.rightPlace = True
        self.onGround = False
        
        
        
    def  __str__(self):
        return "I'm a Player " + str(self.rect.center) + str(self.speed) + str(self.living)
     
    def place(self, pt):
        self.rect.center = pt
     
    def update(*args):
        self = args[0]
        self.lastx = self.realx
        self.lasty = self.realy
        self.jump()
        self.gravity()
        self.move()
        self.move_animate()

        
    def direction(self, dir):  
        if dir == "up":
            self.speed[1] = -self.maxSpeed
            self.dir = "up"
        elif dir == "stop up":
            self.speed[1] = 0
            self.dir = "stop up"
        elif dir == "down":
            self.speed[1] = self.maxSpeed
            self.dir = "down"
        elif dir == "stop down":
            self.speed[1] = 0
            self.dir = "stop down"
        elif dir == "left":
            self.speed[0] = -self.maxSpeed
            self.dir = "left"
            self.canScroll = True
        elif dir == "stop left":
            self.speed[0] = 0
            self.dir = "stop left"
            self.canScroll = False
        elif dir == "right":
            self.speed[0] = self.maxSpeed
            self.dir = "right"
            self.canScroll = True
        elif dir == "stop right":
            self.speed[0] = 0
            self.dir = "stop right"
            self.canScroll = False
        
    def move_animate(self):
        if self.dir[:4] == "stop":
            if self.dir == "stop up":
                self.images = self.backstill
            if self.dir == "stop down":
                self.images = self.frontstill
            if self.dir == "stop left":
                self.images = self.leftstill
            if self.dir == "stop right":
                self.images = self.rightstill
                
            if not self.canShoot:
                self.animate(1)
        else:
            # print "RAN", self.speed
            if self.dir == "up":
                self.images = self.backimgs
            if self.dir == "down":
                self.images = self.frontimgs
            if self.dir == "left":
                self.images = self.leftimgs
            if self.dir == "right":
                self.images = self.rightimgs  
            
            self.animate(3)
         

    def move(self):
        self.realx += self.speed[0]
        self.realy += self.speed[1]
        # print "player", self.realy
        self.x += self.speed[0]
        self.y += self.speed[1]
        
        if self.x >= self.screenWidth - self.xScrollThreshold:
            self.x = self.screenWidth - self.xScrollThreshold
            self.xScrolling = True
        elif self.x <= self.xScrollThreshold:
            self.x = self.xScrollThreshold
            self.xScrolling = True
        else:
            self.xScrolling = False
            
        if self.y >= self.screenHeight - self.yScrollThreshold:
            self.y = self.screenHeight - self.yScrollThreshold
            self.yScrolling = True
        elif self.y <= self.yScrollThreshold:
            self.y = self.yScrollThreshold
            self.y = self.yScrollThreshold
            self.yScrolling = True    
        else:
            self.yScrolling = False
        
        self.place([self.x, self.y])    
        
        
    def blockCollide(self, other):
        print self.lasty, other.realy, self.realy, other.realy
        if (self.lasty <= other.realy) and (self.realy >= other.realy):
            print 'ran'
            self.onGround = True
            # self.realy = other.realy - self.rect.height/2 + other.rect.height/2
            self.y = other.rect.top - self.rect.height/2
            self.speed[1] = 0
            self.canJump = 0
            self.justJump = False
        # elif self.y == other.rect.top - self.rect.height/2:
            # pass
            
        elif self.x < other.realx:
            self.realx -= self.speed[0]
            if not self.xScrolling:
                self.x -= self.speed[0]

        elif self.x > other.realx:
            self.realx += self.speed[0]
            if not self.xScrolling:
                self.x += self.speed[0]
           
    def distToPoint(self, pt):
        x1 = self.rect.center[0]
        x2 = pt[0]
        y1 = self.rect.center[1]
        y2 = pt[1]
        return math.sqrt(((x2-x1)**2)+((y2-y1)**2))
    
    def animate(self, waitTime, cycle = True, place = False):
        self.maxFrame = len(self.images)-1
        if self.waitCount <= waitTime:
            self.waitCount += 1
        else:
            self.waitCount = 0
            if cycle:
                if self.frame > len(self.images)-1:
                    self.frame = len(self.images)-1

                if self.frame == len(self.images)-1:
                    self.frame = 0
                else:
                    self.frame += 1
                            
                self.image = self.images[self.frame] 
                
            if not cycle: 
                if self.frame < self.maxFrame:
                    self.frame += 1
                else:
                    pass
            # if self.dir == "left" or self.dir == "stop left" and place == True:
                # self.rect.topright = self.pos
                # print self.pos
            self.image = self.images[self.frame] 
    
    def shoot(self):
        if self.canShoot:
            if self.rightPlace:
                self.pos = self.rect.topright
                self.rightPlace = False
            if self.dir == "left" or self.dir == "stop left":
                self.images = self.shootleft
            if self.dir == "right" or self.dir == "stop right":
                self.images = self.shootright  
                
            self.animate(5, False, True)  
        else:
            self.rightPlace = True
    
                            
    def jump(self):
        if self.canJump == 1:
            print "ran"
            if not self.justJump:
                self.speed[1] = self.speed[1] = -10
                self.onGround = False
                self.justJump = True
    
    def gravity(self):
        if not self.onGround:
            if self.speed[1] < 50:
                self.speed[1] += 2
            else:
                self.speed[1] = 50
            # pass
        